<template>
  <el-form
    ref="ruleFormRef"
    :model="newFormInline"
    :rules="formRules"
    label-width="120px"
    class="w-full"
  >
    <el-form-item label="设备名称" prop="name">
      <el-input v-model="newFormInline.name" placeholder="请输入名称" />
    </el-form-item>
    <el-form-item label="设备编码" prop="code">
      <el-input v-model="newFormInline.code" placeholder="请输入编码" />
    </el-form-item>
    <el-form-item label="设备类型" prop="type">
      <el-select v-model="newFormInline.type" placeholder="请选择">
        <el-option label="摄像头" :value="1" />
        <el-option label="广播机" :value="2" />
      </el-select>
    </el-form-item>
    <el-form-item label="设备分组" prop="groupId">
      <el-select v-model="newFormInline.groupId" placeholder="请选择">
        <el-option
          v-for="option in props.deviceGroupOptions"
          :key="option.value"
          :label="option.label"
          :value="option.value"
        />
      </el-select>
    </el-form-item>
    <el-form-item label="设备参数" prop="params">
      <el-input
        v-model="newFormInline.params"
        placeholder="请输入参数"
        type="textarea"
      />
    </el-form-item>
    <el-form-item label="备注" prop="comment">
      <el-input
        v-model="newFormInline.comment"
        type="textarea"
        :maxlength="200"
        show-word-limit
        :rows="4"
        placeholder="请输入备注，最多200字"
      />
    </el-form-item>
  </el-form>
</template>

<script setup lang="ts">
import { ref, onMounted } from "vue";
import { FormProps } from "../componentsUtils/type/deviceTypes";
import {
  getFormRules,
  setOriginalDeviceId
} from "../componentsUtils/rule/deviceRules";
const props = withDefaults(
  defineProps<FormProps & { originalDeviceId?: number }>(),
  {
    formInline: () => ({
      id: 0,
      dealerId: 0,
      tenantId: 0,
      groupId: 0,
      zoneId: 0,
      name: "",
      code: "",
      type: 0,
      params: "",
      comment: "",
      isOnline: false
    }),
    title: "",
    deviceGroupOptions: () => [
      {
        label: "默认设备组",
        value: 0
      }
    ],
    originalDeviceId: 0,
    tenantId: 0
  }
);

const newFormInline = ref(props.formInline);
const ruleFormRef = ref();
// 在组件挂载时设置原始设备ID
onMounted(() => {
  setOriginalDeviceId(props.originalDeviceId || 0, props.tenantId || 0);
  newFormInline.value.type = 1;
  //newFormInline.value.groupId = 0;
});
const formRules = getFormRules(
  props.originalDeviceId || 0,
  props.tenantId || 0
);

function getRef() {
  return ruleFormRef.value;
}

defineExpose({ getRef });
</script>
